package top.yudoge.dogeeditor.db.service

import top.yudoge.dogeeditor.db.objects.Schema
import java.sql.Connection

interface SQLVendorBridge {

    fun move(connection: Connection, databaseName: String, schemaName: String) {
        connection.createStatement().use {
            it.execute("USE $databaseName")
        }
        connection.schema = schemaName
    }

    fun schemaList(connection: Connection, dataSourceIdentifier: String, databaseName: String): List<Schema> {
        val schemas = connection.metaData.getSchemas(databaseName, "%")

        return buildList {
            while (schemas.next()) {
                add(
                    Schema(
                        dataSourceIdentifier,
                        databaseName,
                        schemas.getString("TABLE_CAT")
                    )
                )
            }
        }
    }

}